<?php
export_csv();
function export_csv() {
    $filename = date('Y-m-d H:i:s').".csv";
    header("Content-type:text/csv");
    header("Content-Disposition:attachment;filename=".$filename);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    echo array_to_string(get_export_data());
}
function array_to_string($result) {
    if(empty($result)) {
        return i("没有符合您要求的数据！^_^");
    }
    $data = 'username,email'."\n";
    $size_result = sizeof($result);
    for($i = 0 ; $i < $size_result ;  $i++) {
        $data .= i($result[$i]['name']).','.i($result[$i]['option'])."\n";
    }
    return $data;
}
function get_export_data() {
    $link = mysql_connect('localhost','root','') or die(mysql_error());
    mysql_select_db('autodesk');
    $sql = 'select a.username,a.email from jos_users a limit 0,10';
    $result = mysql_query($sql);
    $res = array();
    $i = 0;
    while($row = mysql_fetch_array($result)) {
        $res[$i]['name'] = $row['username'];
        $res[$i]['option'] = $row['email'];
        $i++;
    }
    return $res;
}
function i($strInput) {
    return iconv('utf-8','gb2312',$strInput);//页面编码为utf-8时使用，否则导出的中文为乱码
}
?>
